Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 688)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 12.97554 12.97452 12.97352 12.97253 12.97154 12.97057 12.96960 12.96864
## [9] 12.96767 12.96670 12.96573 12.96476 12.96377 12.96278 12.96177 12.96074
## [17] 12.95970 12.95864 12.95756 12.95646 12.95532 12.95416 12.95297 12.95175
## [25] 12.95049 12.94919 12.94785 12.94647 12.94505 12.94358 12.94206 12.94049
## [33] 12.93887 12.93719 12.93545 12.93366 12.93179 12.92982 12.92778 12.92566
## [41] 12.92346 12.92121 12.91889 12.91652 12.91411 12.91166 12.90917 12.90666
## [49] 12.90413 12.90158 12.89902 12.89646 12.89391 12.89136 12.88883 12.88633
## [57] 12.88385 12.88141 12.87900 12.87665 12.87435 12.87211 12.86993 12.86783
## [65] 12.86580 12.86386 12.86201 12.86026 12.85861 12.85707 12.85564 12.85409
## [73] 12.85217 12.84990 12.84731 12.84442 12.84124 12.83779 12.83411 12.83019
## [81] 12.82608 12.82178 12.81732 12.81271 12.80798 12.80315 12.79824 12.79326
## [89] 12.78825 12.78321 12.77817 12.77315 12.76817 12.76324 12.75840 12.75366
## [97] 12.74903 12.74455 12.74023 12.73609 12.73214 12.72842 12.72495 12.72173
## [105] 12.71879 12.71616 12.71385 12.71188 12.71028 12.70905 12.70823 12.70784
## [113] 12.70788 12.70826 12.70883 12.70961 12.71059 12.71176 12.71314 12.71473
## [121] 12.71651 12.71851 12.72070 12.72311 12.72572 12.72854 12.73157 12.73481
## [129] 12.73827 12.74193 12.74580 12.74989 12.75420 12.75871 12.76345 12.76840
## [137] 12.77357 12.77895 12.78456 12.79038 12.79759 12.80717 12.81890 12.83253
## [145] 12.84782 12.86453 12.88243 12.90127 12.92082 12.94083 12.96107 12.98130
## [153] 13.00127 13.02076 13.03951 13.05730 13.07387 13.08900 13.10245 13.11396
## [161] 13.12332 13.13256 13.14375 13.15665 13.17105 13.18670 13.20337 13.22084
## [169] 13.23887 13.25723 13.27570 13.29403 13.31200 13.32938 13.34594 13.36144
## [177] 13.37565 13.38835 13.39930 13.40827 13.41504 13.41936 13.42256 13.42606
## [185] 13.42977 13.43359 13.43745 13.44125 13.44489 13.44830 13.45139 13.45405
## [193] 13.45621 13.45778 13.45866 13.45878 13.45802 13.45632 13.45358 13.44971
## [201] 13.44462 13.43822 13.43043 13.42115 13.41030 13.39707 13.38094 13.36217
## [209] 13.34104 13.31783 13.29283 13.26630 13.23854 13.20981 13.18041 13.15060
## [217] 13.12066 13.09089 13.06154 13.03291 13.00528 12.97891 12.95410 12.93112
## [225] 12.90717 12.87955 12.84864 12.81484 12.77855 12.74015 12.70004 12.65862
## [233] 12.61627 12.57340 12.53039 12.48764 12.44554 12.40449 12.36487 12.32709
## [241] 12.29154 12.25861 12.22869 12.20218 12.17947 12.15926 12.13995 12.12148
## [249] 12.10381 12.08688 12.07066 12.05508 12.04011 12.02568 12.01176 11.99828
## [257] 11.98521 11.97250 11.96008 11.94792 11.93597 11.92557 11.91793 11.91279
## [265] 11.90988 11.90895 11.90973 11.91195 11.91535 11.91967 11.92465 11.93003
## [273] 11.93553 11.94090 11.94587 11.95018 11.95357 11.95577 11.95652 11.95556
## [281] 11.95262 11.94744 11.94150 11.93636 11.93193 11.92809 11.92475 11.92180
## [289] 11.91914 11.91665 11.91424 11.91179 11.90921 11.90639 11.90322 11.89960
## [297] 11.89543 11.89059 11.88499 11.87851 11.87106 11.86244 11.85261 11.84170
## [305] 11.82981 11.81706 11.80359 11.78950 11.77492 11.75996 11.74475 11.72939
## [313] 11.71403 11.69876 11.68371 11.66901 11.65476 11.64109 11.62812 11.61596
## [321] 11.60475 11.59458 11.58272 11.56673 11.54728 11.52500 11.50054 11.47454
## [329] 11.44765 11.42051 11.39375 11.36804 11.34400 11.32229 11.30355 11.28842
## [337] 11.27754 11.27157 11.26827 11.26505 11.26199 11.25918 11.25669 11.25461
## [345] 11.25302 11.25200 11.25164 11.25201 11.25320 11.25529 11.25837 11.26251
## [353] 11.26779 11.27431 11.28213 11.29135 11.30204 11.31516 11.33142 11.35054
## [361] 11.37227 11.39633 11.42245 11.45036 11.47979 11.51048 11.54214 11.57452
## [369] 11.60733 11.64032 11.67321 11.70574 11.73762 11.76860 11.79840 11.82675
## [377] 11.85338 11.87803 11.90345 11.93233 11.96429 11.99896 12.03594 12.07486
## [385] 12.11534 12.15700 12.19946 12.24233 12.28525 12.32782 12.36967 12.41041
## [393] 12.44967 12.48706 12.52222 12.55474 12.58426 12.61040 12.63277 12.65416
## [401] 12.67738 12.70208 12.72788 12.75443 12.78137 12.80833 12.83497 12.86091
## [409] 12.88580 12.90928 12.93098 12.95055 12.96763 12.98185 12.99285 13.00088
## [417] 13.00653 13.00997 13.01136 13.01090 13.00874 13.00507 13.00005 12.99385
## [425] 12.98666 12.97865 12.96998 12.96083 12.95137 12.94178 12.93224 12.92290
## [433] 12.91395 12.90556 12.89789 12.89114 12.88316 12.87195 12.85785 12.84121
## [441] 12.82239 12.80173 12.77958 12.75628 12.73220 12.70768 12.68306 12.65869
## [449] 12.63493 12.61213 12.59063 12.57077 12.55292 12.53742 12.52462 12.51258
## [457] 12.49924 12.48475 12.46927 12.45296 12.43599 12.41850 12.40065 12.38261
## [465] 12.36454 12.34658 12.32890 12.31166 12.29501 12.27911 12.26413 12.25021
## [473] 12.23752 12.22622 12.21646 12.20840 12.20133 12.19444 12.18778 12.18137
## [481] 12.17526 12.16947 12.16406 12.15905 12.15449 12.15040 12.14683 12.14381
## [489] 12.14137 12.13957 12.13842 12.13797 12.13845 12.13999 12.14253 12.14599
## [497] 12.15029 12.15537 12.16114 12.16752 12.17445 12.18185 12.18965 12.19776
## [505] 12.20611 12.21463 12.22325 12.23188 12.24045 12.24889 12.25713 12.26662
## [513] 12.27870 12.29309 12.30951 12.32771 12.34738 12.36827 12.39009 12.41258
## [521] 12.43544 12.45842 12.48123 12.50360 12.52525 12.54591 12.56530 12.58315
## [529] 12.59918 12.61311 12.62467 12.63359 12.64166 12.65081 12.66089 12.67179
## [537] 12.68339 12.69555 12.70815 12.72108 12.73420 12.74739 12.76052 12.77347
## [545] 12.78612 12.79835 12.81002 12.82101 12.83120 12.84046 12.84867 12.85571
## [553] 12.86145 12.86576 12.86852 12.86961 12.86891 12.86627 12.86160 12.85475
## [561] 12.84527 12.83301 12.81828 12.80140 12.78268 12.76246 12.74103 12.71873
## [569] 12.69587 12.67276 12.64973 12.62709 12.60516 12.58425 12.56469 12.54680
## [577] 12.52705 12.50212 12.47260 12.43910 12.40219 12.36249 12.32058 12.27706
## [585] 12.23253 12.18758 12.14281 12.09882 12.05619 12.01553 11.97742 11.94247
## [593] 11.91128 11.88443 11.86252 11.84196 11.81907 11.79428 11.76806 11.74084
## [601] 11.71310 11.68527 11.65781 11.63116 11.60579 11.58213 11.56065 11.54178
## [609] 11.52600 11.51373 11.50427 11.49650 11.49033 11.48566 11.48237 11.48037
## [617] 11.47956 11.47982 11.48106 11.48317 11.48604 11.48958 11.49369 11.49825
## [625] 11.50316 11.50833 11.51364 11.51899 11.52428 11.52941 11.53427 11.53876
## [633] 11.54346 11.54899 11.55534 11.56247 11.57037 11.57900 11.58835 11.59839
## [641] 11.60909 11.62043 11.63239 11.64494 11.65806 11.67172 11.68589 11.70056
## [649] 11.71570 11.73128 11.74728 11.76386 11.78117 11.79920 11.81792 11.83731
## [657] 11.85736 11.87804 11.89934 11.92124 11.94371 11.96673 11.99029 12.01436
## [665] 12.03893 12.06398 12.08948 12.11542 12.14180 12.16864 12.19594 12.22372
## [673] 12.25197 12.28071 12.30995 12.33970 12.36997 12.40076 12.43209 12.46396
## [681] 12.49638 12.52936 12.56291 12.59705 12.63177 12.66709 12.70302 12.73956
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 688)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.61241 12.60848 12.60464 12.60089 12.59722 12.59364 12.59015 12.58673
## [9] 12.58340 12.58015 12.57697 12.57386 12.57083 12.56787 12.56497 12.56214
## [17] 12.55938 12.55668 12.55404 12.55147 12.54894 12.54648 12.54407 12.54170
## [25] 12.53939 12.53713 12.53491 12.53274 12.53061 12.52852 12.52647 12.52445
## [33] 12.52247 12.52053 12.51861 12.51672 12.51484 12.51294 12.51103 12.50912
## [41] 12.50721 12.50530 12.50341 12.50154 12.49970 12.49788 12.49611 12.49437
## [49] 12.49269 12.49106 12.48950 12.48800 12.48658 12.48523 12.48397 12.48281
## [57] 12.48174 12.48077 12.47991 12.47917 12.47855 12.47806 12.47770 12.47749
## [65] 12.47741 12.47749 12.47773 12.47813 12.47869 12.47944 12.48036 12.48141
## [73] 12.48252 12.48369 12.48493 12.48623 12.48760 12.48904 12.49055 12.49213
## [81] 12.49379 12.49552 12.49734 12.49923 12.50120 12.50326 12.50540 12.50762
## [89] 12.50994 12.51235 12.51484 12.51743 12.52012 12.52290 12.52579 12.52877
## [97] 12.53185 12.53504 12.53833 12.54173 12.54524 12.54886 12.55260 12.55644
## [105] 12.56041 12.56449 12.56869 12.57301 12.57745 12.58202 12.58671 12.59153
## [113] 12.59648 12.60141 12.60618 12.61080 12.61530 12.61970 12.62403 12.62829
## [121] 12.63252 12.63673 12.64095 12.64519 12.64948 12.65384 12.65829 12.66284
## [129] 12.66753 12.67236 12.67737 12.68257 12.68799 12.69364 12.69955 12.70676
## [137] 12.71617 12.72757 12.74073 12.75544 12.77150 12.78868 12.80677 12.82556
## [145] 12.84484 12.86438 12.88397 12.90341 12.92247 12.94094 12.95861 12.97526
## [153] 12.99068 13.00465 13.01696 13.02740 13.03851 13.05273 13.06965 13.08891
## [161] 13.11009 13.13282 13.15670 13.18133 13.20634 13.23132 13.25590 13.27967
## [169] 13.30224 13.32323 13.34224 13.35889 13.37278 13.38352 13.39073 13.39587
## [177] 13.40067 13.40511 13.40916 13.41279 13.41598 13.41870 13.42093 13.42264
## [185] 13.42381 13.42441 13.42440 13.42378 13.42251 13.42056 13.41792 13.41455
## [193] 13.41043 13.40553 13.39983 13.39330 13.38592 13.37766 13.36849 13.35839
## [201] 13.34733 13.33529 13.32224 13.30816 13.29117 13.26984 13.24478 13.21659
## [209] 13.18588 13.15328 13.11938 13.08481 13.05017 13.01607 12.98312 12.95194
## [217] 12.92314 12.89732 12.87510 12.85309 12.82774 12.79941 12.76846 12.73526
## [225] 12.70018 12.66357 12.62581 12.58725 12.54826 12.50920 12.47044 12.43234
## [233] 12.39527 12.35959 12.32567 12.29386 12.26454 12.23806 12.21479 12.19510
## [241] 12.17777 12.16133 12.14574 12.13094 12.11691 12.10359 12.09095 12.07894
## [249] 12.06752 12.05664 12.04627 12.03636 12.02688 12.01776 12.00899 12.00051
## [257] 11.99227 11.98425 11.97638 11.97027 11.96730 11.96718 11.96960 11.97426
## [265] 11.98086 11.98908 11.99864 12.00922 12.02053 12.03225 12.04409 12.05574
## [273] 12.06690 12.07726 12.08652 12.09439 12.10054 12.10469 12.10653 12.10575
## [281] 12.10478 12.10603 12.10915 12.11378 12.11958 12.12621 12.13330 12.14053
## [289] 12.14753 12.15396 12.15947 12.16372 12.16635 12.16702 12.16537 12.16107
## [297] 12.15444 12.14614 12.13632 12.12513 12.11271 12.09923 12.08482 12.06964
## [305] 12.05384 12.03756 12.02097 12.00420 11.98740 11.97074 11.95435 11.93838
## [313] 11.92299 11.90833 11.89454 11.88178 11.87019 11.85738 11.84111 11.82179
## [321] 11.79984 11.77567 11.74968 11.72229 11.69391 11.66494 11.63580 11.60690
## [329] 11.57864 11.55145 11.52572 11.50187 11.48031 11.46144 11.44569 11.43345
## [337] 11.42515 11.41836 11.41056 11.40203 11.39303 11.38384 11.37473 11.36596
## [345] 11.35781 11.35055 11.34444 11.33976 11.33677 11.33575 11.33696 11.34068
## [353] 11.34708 11.35602 11.36730 11.38070 11.39603 11.41308 11.43163 11.45149
## [361] 11.47245 11.49429 11.51681 11.53982 11.56309 11.58642 11.60961 11.63246
## [369] 11.65474 11.67626 11.69681 11.71619 11.73669 11.76052 11.78738 11.81691
## [377] 11.84881 11.88275 11.91839 11.95542 11.99350 12.03231 12.07152 12.11081
## [385] 12.14985 12.18832 12.22588 12.26221 12.29699 12.32989 12.36058 12.38873
## [393] 12.41403 12.43613 12.45742 12.48030 12.50455 12.52991 12.55615 12.58303
## [401] 12.61031 12.63774 12.66510 12.69213 12.71861 12.74428 12.76891 12.79226
## [409] 12.81409 12.83416 12.85222 12.86805 12.88139 12.89313 12.90427 12.91481
## [417] 12.92474 12.93403 12.94267 12.95065 12.95795 12.96457 12.97047 12.97566
## [425] 12.98011 12.98382 12.98675 12.98891 12.99028 12.99084 12.99057 12.98947
## [433] 12.98752 12.98470 12.98100 12.97504 12.96575 12.95356 12.93890 12.92220
## [441] 12.90389 12.88440 12.86417 12.84363 12.82320 12.80333 12.78443 12.76695
## [449] 12.75131 12.73795 12.72429 12.70767 12.68842 12.66686 12.64332 12.61810
## [457] 12.59153 12.56394 12.53564 12.50695 12.47821 12.44971 12.42180 12.39478
## [465] 12.36899 12.34473 12.32234 12.30213 12.28442 12.26954 12.25781 12.24694
## [473] 12.23462 12.22107 12.20652 12.19119 12.17533 12.15916 12.14290 12.12679
## [481] 12.11106 12.09594 12.08166 12.06844 12.05652 12.04613 12.03750 12.03085
## [489] 12.02641 12.02442 12.02425 12.02508 12.02685 12.02950 12.03296 12.03719
## [497] 12.04211 12.04768 12.05382 12.06049 12.06761 12.07514 12.08301 12.09115
## [505] 12.09952 12.10806 12.11860 12.13280 12.15031 12.17077 12.19382 12.21910
## [513] 12.24627 12.27495 12.30479 12.33544 12.36655 12.39774 12.42867 12.45898
## [521] 12.48831 12.51630 12.54260 12.56686 12.58871 12.60779 12.62376 12.63915
## [529] 12.65662 12.67597 12.69698 12.71946 12.74321 12.76801 12.79367 12.81998
## [537] 12.84673 12.87373 12.90076 12.92763 12.95414 12.98006 13.00522 13.02939
## [545] 13.05237 13.07397 13.09397 13.11218 13.12839 13.14239 13.15398 13.16297
## [553] 13.16913 13.17228 13.17220 13.16925 13.16406 13.15679 13.14762 13.13672
## [561] 13.12426 13.11044 13.09540 13.07934 13.06243 13.04484 13.02675 13.00832
## [569] 12.98975 12.97119 12.95283 12.93483 12.91739 12.90066 12.88209 12.85928
## [577] 12.83264 12.80257 12.76949 12.73381 12.69593 12.65628 12.61525 12.57326
## [585] 12.53072 12.48803 12.44562 12.40389 12.36324 12.32410 12.28686 12.25195
## [593] 12.21977 12.19072 12.16523 12.13942 12.10962 12.07650 12.04076 12.00308
## [601] 11.96415 11.92465 11.88527 11.84670 11.80963 11.77473 11.74271 11.71423
## [609] 11.69000 11.67069 11.65453 11.63927 11.62491 11.61144 11.59886 11.58716
## [617] 11.57633 11.56637 11.55728 11.54905 11.54167 11.53514 11.52945 11.52460
## [625] 11.52058 11.51739 11.51502 11.51346 11.51271 11.51276 11.51362 11.51526
## [633] 11.51769 11.52089 11.52489 11.52968 11.53527 11.54168 11.54891 11.55696
## [641] 11.56586 11.57560 11.58620 11.59766 11.60999 11.62320 11.63730 11.65229
## [649] 11.66820 11.68501 11.70275 11.72138 11.74086 11.76120 11.78239 11.80443
## [657] 11.82733 11.85109 11.87570 11.90117 11.92749 11.95467 11.98271 12.01161
## [665] 12.04137 12.07198 12.10345 12.13578 12.16894 12.20295 12.23779 12.27348
## [673] 12.31001 12.34738 12.38559 12.42465 12.46455 12.50530 12.54689 12.58932
## [681] 12.63260 12.67672 12.72169 12.76751 12.81417 12.86168 12.91003 12.95924
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 688)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.95094 11.94933 11.94775 11.94620 11.94468 11.94318 11.94171 11.94025
## [9] 11.93881 11.93738 11.93595 11.93454 11.93312 11.93170 11.93028 11.92886
## [17] 11.92742 11.92597 11.92450 11.92302 11.92151 11.91998 11.91841 11.91682
## [25] 11.91519 11.91352 11.91181 11.91006 11.90826 11.90641 11.90451 11.90254
## [33] 11.90052 11.89844 11.89629 11.89407 11.89178 11.88942 11.88697 11.88445
## [41] 11.88184 11.87914 11.87635 11.87342 11.87029 11.86699 11.86352 11.85991
## [49] 11.85617 11.85231 11.84834 11.84429 11.84016 11.83597 11.83174 11.82747
## [57] 11.82319 11.81891 11.81464 11.81040 11.80621 11.80206 11.79799 11.79401
## [65] 11.79013 11.78636 11.78272 11.77922 11.77589 11.77273 11.76975 11.76698
## [73] 11.76442 11.76210 11.76002 11.75821 11.75667 11.75541 11.75406 11.75222
## [81] 11.74992 11.74721 11.74411 11.74066 11.73689 11.73283 11.72853 11.72400
## [89] 11.71929 11.71442 11.70944 11.70437 11.69925 11.69411 11.68898 11.68390
## [97] 11.67890 11.67402 11.66928 11.66473 11.66039 11.65630 11.65249 11.64899
## [105] 11.64584 11.64308 11.64072 11.63882 11.63740 11.63649 11.63613 11.63635
## [113] 11.63718 11.63867 11.64083 11.64371 11.64734 11.65175 11.65697 11.66304
## [121] 11.67061 11.68018 11.69156 11.70459 11.71908 11.73486 11.75173 11.76953
## [129] 11.78807 11.80718 11.82667 11.84637 11.86609 11.88565 11.90488 11.92360
## [137] 11.94162 11.95878 11.97488 11.98974 12.00568 12.02490 12.04707 12.07186
## [145] 12.09894 12.12799 12.15867 12.19065 12.22362 12.25723 12.29117 12.32510
## [153] 12.35869 12.39161 12.42355 12.45416 12.48312 12.51010 12.53477 12.55680
## [161] 12.57587 12.59424 12.61425 12.63568 12.65829 12.68185 12.70616 12.73097
## [169] 12.75606 12.78121 12.80619 12.83078 12.85474 12.87785 12.89989 12.92064
## [177] 12.93985 12.95732 12.97281 12.98609 12.99695 13.00515 13.01195 13.01871
## [185] 13.02534 13.03178 13.03793 13.04372 13.04907 13.05390 13.05813 13.06169
## [193] 13.06448 13.06644 13.06748 13.06753 13.06650 13.06432 13.06090 13.05617
## [201] 13.05005 13.04246 13.03331 13.02254 13.01005 12.99419 12.97366 12.94895
## [209] 12.92055 12.88896 12.85466 12.81815 12.77991 12.74044 12.70023 12.65976
## [217] 12.61954 12.58004 12.54176 12.50518 12.47081 12.43914 12.41064 12.38581
## [225] 12.36136 12.33389 12.30374 12.27126 12.23678 12.20065 12.16321 12.12480
## [233] 12.08578 12.04647 12.00722 11.96838 11.93028 11.89328 11.85770 11.82390
## [241] 11.79222 11.76299 11.73657 11.71329 11.69350 11.67630 11.66049 11.64592
## [249] 11.63247 11.61999 11.60836 11.59744 11.58709 11.57719 11.56758 11.55815
## [257] 11.54875 11.53925 11.52952 11.51941 11.50880 11.49958 11.49353 11.49034
## [265] 11.48971 11.49133 11.49490 11.50012 11.50667 11.51426 11.52258 11.53132
## [273] 11.54018 11.54886 11.55705 11.56445 11.57074 11.57564 11.57882 11.57999
## [281] 11.57885 11.57508 11.57018 11.56577 11.56180 11.55819 11.55487 11.55175
## [289] 11.54878 11.54588 11.54298 11.53999 11.53686 11.53351 11.52987 11.52586
## [297] 11.52141 11.51645 11.51091 11.50471 11.49779 11.48880 11.47670 11.46179
## [305] 11.44441 11.42487 11.40351 11.38064 11.35660 11.33170 11.30627 11.28063
## [313] 11.25511 11.23003 11.20572 11.18249 11.16068 11.14061 11.12260 11.10697
## [321] 11.09406 11.08418 11.07476 11.06325 11.05004 11.03550 11.01999 11.00390
## [329] 10.98760 10.97147 10.95587 10.94118 10.92778 10.91604 10.90634 10.89905
## [337] 10.89454 10.89319 10.89446 10.89749 10.90216 10.90833 10.91589 10.92472
## [345] 10.93469 10.94567 10.95756 10.97021 10.98352 10.99735 11.01158 11.02610
## [353] 11.04077 11.05548 11.07011 11.08452 11.09859 11.11383 11.13165 11.15182
## [361] 11.17410 11.19825 11.22405 11.25125 11.27963 11.30894 11.33896 11.36944
## [369] 11.40016 11.43087 11.46135 11.49136 11.52066 11.54902 11.57621 11.60198
## [377] 11.62611 11.64836 11.67053 11.69446 11.71993 11.74674 11.77468 11.80356
## [385] 11.83317 11.86330 11.89375 11.92432 11.95479 11.98498 12.01466 12.04364
## [393] 12.07172 12.09868 12.12433 12.14846 12.17087 12.19135 12.20969 12.22775
## [401] 12.24731 12.26809 12.28980 12.31217 12.33492 12.35775 12.38038 12.40254
## [409] 12.42394 12.44430 12.46333 12.48076 12.49629 12.50966 12.52056 12.52945
## [417] 12.53700 12.54329 12.54841 12.55242 12.55540 12.55745 12.55863 12.55903
## [425] 12.55872 12.55778 12.55630 12.55435 12.55200 12.54935 12.54646 12.54342
## [433] 12.54030 12.53719 12.53417 12.53130 12.52739 12.52133 12.51332 12.50358
## [441] 12.49233 12.47980 12.46619 12.45172 12.43661 12.42108 12.40535 12.38963
## [449] 12.37413 12.35908 12.34469 12.33119 12.31878 12.30768 12.29812 12.28872
## [457] 12.27808 12.26632 12.25357 12.23998 12.22567 12.21078 12.19544 12.17979
## [465] 12.16396 12.14808 12.13229 12.11672 12.10151 12.08679 12.07268 12.05934
## [473] 12.04688 12.03545 12.02518 12.01620 12.00728 11.99727 11.98635 11.97474
## [481] 11.96264 11.95025 11.93777 11.92542 11.91340 11.90190 11.89114 11.88132
## [489] 11.87264 11.86530 11.85952 11.85550 11.85237 11.84918 11.84600 11.84290
## [497] 11.83994 11.83718 11.83470 11.83256 11.83083 11.82957 11.82885 11.82874
## [505] 11.82929 11.83059 11.83269 11.83566 11.83958 11.84449 11.85047 11.85823
## [513] 11.86827 11.88038 11.89434 11.90994 11.92695 11.94516 11.96436 11.98432
## [521] 12.00484 12.02568 12.04665 12.06751 12.08805 12.10806 12.12732 12.14562
## [529] 12.16272 12.17843 12.19251 12.20477 12.21714 12.23163 12.24803 12.26615
## [537] 12.28581 12.30680 12.32893 12.35201 12.37585 12.40026 12.42504 12.45000
## [545] 12.47495 12.49969 12.52404 12.54779 12.57076 12.59276 12.61358 12.63304
## [553] 12.65095 12.66712 12.68134 12.69343 12.70319 12.71044 12.71498 12.71661
## [561] 12.71551 12.71216 12.70678 12.69962 12.69093 12.68093 12.66988 12.65802
## [569] 12.64558 12.63281 12.61995 12.60724 12.59492 12.58323 12.57241 12.56272
## [577] 12.55152 12.53635 12.51763 12.49578 12.47122 12.44439 12.41571 12.38560
## [585] 12.35449 12.32281 12.29098 12.25943 12.22858 12.19886 12.17069 12.14450
## [593] 12.12072 12.09977 12.08207 12.06428 12.04313 12.01918 11.99300 11.96514
## [601] 11.93618 11.90667 11.87718 11.84827 11.82050 11.79443 11.77064 11.74967
## [609] 11.73210 11.71849 11.70754 11.69755 11.68850 11.68033 11.67300 11.66648
## [617] 11.66072 11.65568 11.65131 11.64759 11.64446 11.64188 11.63981 11.63822
## [625] 11.63706 11.63629 11.63586 11.63574 11.63588 11.63625 11.63680 11.63748
## [633] 11.63858 11.64035 11.64279 11.64587 11.64956 11.65386 11.65873 11.66415
## [641] 11.67012 11.67659 11.68355 11.69099 11.69888 11.70719 11.71591 11.72502
## [649] 11.73449 11.74431 11.75444 11.76500 11.77610 11.78772 11.79985 11.81250
## [657] 11.82566 11.83932 11.85347 11.86810 11.88322 11.89880 11.91486 11.93138
## [665] 11.94835 11.96576 11.98362 12.00190 12.02058 12.03968 12.05920 12.07915
## [673] 12.09952 12.12032 12.14156 12.16325 12.18538 12.20796 12.23100 12.25450
## [681] 12.27847 12.30290 12.32781 12.35320 12.37907 12.40544 12.43230 12.45965
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")